﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using System.Data;

namespace EngineeringMaster
{
    public partial class EditTeacherInfo : System.Web.UI.Page
    {
        private string teacherId;
        private TeacherDAO teacherDao = new TeacherDAO();
        protected void Page_Load(object sender, EventArgs e)
        {

            teacherId = Session["teacherId"].ToString().Trim();
            if (teacherId == null || teacherId.Length < 1)
            {
                return;
            }
            if(!this.IsPostBack)
            {
                loadInfo();
            }
        }

        public void loadInfo()
        {
            DataRow dr = teacherDao.getTeacherByID(teacherId).Tables[0].Rows[0];
            this.lbId.Text = dr[0].ToString().Trim();
            this.lbName.Text = dr[1].ToString().Trim();
            this.lbAddress.Text = dr[2].ToString().Trim();
            this.lbTelephone.Text = dr[3].ToString().Trim();
            this.lbFax.Text = dr[4].ToString().Trim();
            this.lbEmail.Text = dr[5].ToString().Trim();
            this.lbTitle.Text = dr[6].ToString().Trim();
            this.lbDegree.Text = dr[8].ToString().Trim();
            this.lbResearchArea.Text = dr[9].ToString().Trim();
            this.lbJobBackground.Text = dr[10].ToString().Trim();
            this.teacherImg.ImageUrl = dr[11].ToString().Trim();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string teacherId = this.lbId.Text; 
           
            string teacherName = this.lbName.Text;
            string teacherAddress = this.lbAddress.Text ;
            string teacherTelephone = this.lbTelephone.Text;
            string teacherFax = this.lbFax.Text;
            string teacherEmail = this.lbEmail.Text ;
            string teacherTitle = this.lbTitle.Text ;
            string teacherDegree = this.lbDegree.Text;
            string teacherResearch = this.lbResearchArea.Text;
            string teacherJob = this.lbJobBackground.Text;
            if(! teacherDao.updateTeacherInfo(teacherName, teacherAddress, teacherTelephone,
                teacherFax, teacherEmail, teacherTitle, teacherDegree, teacherResearch, teacherJob, teacherId))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('更新失败, 请填写完整后重试.')</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('更新成功。')</script>");
            }
        }



        protected void Button2_Click(object sender, EventArgs e)
        {
            if(this.FileUpload2.HasFile)
            {
                string filePath = this.FileUpload2.FileName;
                if (!filePath.EndsWith(".jpg") && !filePath.EndsWith(".gif") && filePath.EndsWith(".png"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件格式有误。')</script>");
                    return;
                }
                try
                {
                    string fullPath = Server.MapPath("~") + "Img\\" + filePath;
                    FileUpload2.SaveAs(fullPath);
                    int i = fullPath.IndexOf("Img");
                    fullPath = fullPath.Substring(i);
                    string temp = "~\\\\";
                    fullPath = temp + fullPath;
                    if(teacherDao.updateTeacherImg(teacherId, fullPath))
                    {
                        this.loadInfo();
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('更新成功.')</script>");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('更新失败。')</script>");
                    }
                    
                }
                catch (System.Exception ex)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件上传出错啦。')</script>");
                }
            }
        }
    }
}
